//GET请求
function GET(requestHandler) {
	request('GET', requestHandler)
}
//DELETE请求
function DELETE(requestHandler) {
	request('delete', requestHandler)
}
//POST请求
function POST(requestHandler) {
	request('POST', requestHandler)
}
// PUT请求
function PUT(requestHandler) {
	request('PUT', requestHandler)
}

function file(requestHandler) {
	uploadFile(requestHandler)
}
// let ip = 'pm.yunhuzh.com'
let ip = 'www.shenzhendinghanpaimai.cn'
function request(method, requestHandler) {
	var url = requestHandler.url;
	let prefix = requestHandler.prefix ? requestHandler.prefix : '/Api'
	//注意：可以对params加密等处理
	var params = requestHandler.data ? requestHandler.data : {};
	let header = requestHandler.header ? requestHandler.header : {
		"content-type": "application/json",
		Authorization: 'Bearer ' + wx.getStorageSync('token')
	}
	wx.request({
		url: 'https://' + ip + prefix + url,
		data: params,
		method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
		header: header, // 设置请求的 header
		success: (res) => {
			//注意：可以对参数解密等处理
			if (res.data.code == 401) {
			
			} else if (res.data.code != 0) {
	
			}
			requestHandler.then(res)
		},
		// fail: function (error) {
		//   requestHandler.catch(error)
		// },
		// complete: function () {
		//   // complete
		// }
	})
}

function uploadFile(requestHandler) {
	var url = requestHandler.url;
	var filePath = requestHandler.img;
	var formData = requestHandler.data ? requestHandler.data : {};
	let prefix = requestHandler.prefix ? requestHandler.prefix : '/Api'
	console.log(filePath);
	wx.uploadFile({
		// url: 'http://' + ip + '/admin-api' + url,
		url: 'https://' + ip + prefix + url,
		filePath: filePath,
		formData: formData,
		name: 'avatarFile',
		header: {
			Authorization: 'Bearer ' + wx.getStorageSync('token'),
			'content-type': 'multipart/form-data',
		},
		success: function (res) {
			//注意：可以对参数解密等处理
			if (res.data.code == 401) {
				console.log('执行了');
				wx.navigateTo({
					url: `/pages/login/login`
				})
				wx.showToast({
					title: '登录过期',
					icon: 'error',
					duration: 2000
				})
			}
			requestHandler.then(res)
		},
		fail: function (error) {
			console.log(error);
		},
		// complete: function () {
		//   // complete
		// }
	})
}
module.exports = {
	get: GET,
	delete: DELETE,
	post: POST,
	put: PUT,
	file: file,
	ip: ip,
}